import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
// ===================================================================== Declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const TimeSet 	= $Lazy.state(()=>import('../../cpt/time-set'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const checkbox = { type:'checkbox', mb:10, value:0}
// ===================================================================== component
class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			{ label: '项目接收注意事项',						name: 'receive_pro_notes', full:true, type:'textarea' },
			{ label:' ', tag: '不能拆分项目',						name: 'is_split_pro', 	 	...checkbox },
			{ label:'', tag:'提醒输入采样时间，体位等特殊要求',		name: 'is_remind_input', 	...checkbox, width:300 },
			{ label:'', tag:'联机项目',								name: 'is_online_pro', 		...checkbox },
			{ label:'单个批次项目辅助标准工时',				name: 'pro_help_time_num', 		half:true, br:true  },
			{ label:'单个批次项目检测人员标准工时',			name: 'pro_inspector_time_num', half:true  },
			{ label:'实验号模板',							name: 'low_board_id', 			half:true, type:'select', loading:true, required:true },
			{ label:'项目标签模板',							name: 'tag_id', 				half:true, type:'select', loading:true, required:true  },
			// 检测时间设置
			{ label: '单个检测周期（小时）',					name: 'test_cycle_time_num', 	half:true, type:'number', value:0, required:true, title:'检测时间设置'  },
			{ label: '承诺发报告时间（小时）',				name: 'send_time_num', 			half:true, type:'number', value:0, required:true  },
		],
		fd:[]
	}
	
	componentDidMount(){
		this.submit = { kind_id: this.props.kind_id }
		this.getSelect()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 结果提示类型
			$fn.setRow( formData, 'tag_id',  await f.getCache({ api: 'tag_template' }) )
			// 项目标签模板
			$fn.setRow( formData, 'low_board_id',  await f.getCache({ api: 'num_template' }) )
			
			//
			this.setState({ formData })
		})
	}
	
	onSubmit = () => {
		this.timeRef.onSubmit()
		setTimeout(()=>this.formRef.submit())
	}
	
	fetch = () => {
		$http.submit(null,'dist-check-cats/info', { param:{uuid:this.props.kind_id}, isPull:true, onError:()=> this.timeRef?.load?.([]) } ).then((data)=>{
			this.formRef.setValue(data)
			const { report_time_set } = data
			this.timeRef?.load?.(report_time_set)
		})
	}
	
	// report_time_set
	fd = {}
	
	render(){
		const { edit } = this.props
		return (
			<>
				<Box contentClassName='pt10 pr10 pl10' first>
					{ 
						SubmitForm.call(this,{ 
							msg:'保存', labelWidth:'w200', api:'dist-check-cats/add', onForm: this.fetch, 
							getSubmit:()=>{		// 处理提交数据
								const _fd = []
								for(let i in this.fd){
									if(!$fn.isValid(this.fd[i]['postpone'])){
										this.fd[i]['postpone'] = ''
									}
									_fd.push(this.fd[i])
								}
								return {report_time_set:_fd}
							},
							onOk:this.props.onAudit,
							disabled:edit
						})
					}
				</Box>
				{/* 发报告单时间设置 */}
				<TimeSet 
					onRef		= { ref=> this.timeRef=ref }
					onSubmit 	= { value => {
						this.fd = { ...this.fd, ...value }
					}}
					disabled	= { edit }
				/>
			</>
		)
	}
}

export default Index